#!/usr/bin/python
# -*- coding: utf-8 -*-

import Image
import sys


def splitImage(file, output, ow, oh):
	im = Image.open(file)
	nim = im.copy()
	(width, height) = im.size

#1. 2値化
        nim = nim.convert("1")
	
#2. 行SCAN＆統計
	line_sum = []
	pix = nim.load()
	for x in range(height):
		cnt = 0
                for y in range(width):
                       if pix[y, x] == 0:
				cnt = cnt + 1 
		line_sum.append(cnt)

	for v in line_sum:
		print v

#3. 騒音対策
	min_pix = 10	#決め方要検討
	for x in range(height):
		if ( line_sum[x] <= min_pix ):
			line_sum[x] = 0
		else:
			line_sum[x] = line_sum[x] - min_pix 


#4. DOCの正当性チェック(SKIP)

#5. 行分け (zero_counter > 1)
	blocks = []
	tStart = 0
	tEnd   = 0
	bFirst = True
	zero_counter = 0 
	for x in range(height):
		if ( line_sum[x] != 0 ):
			if ( bFirst ) :
				bFirst = False
				tStart = x
			zero_counter = 0
		else:
			if ( not bFirst and zero_counter > 1 ) :
				tEnd = x			
				bFirst = True
				blocks.append((tStart, tEnd))
			zero_counter = zero_counter + 1

#6.行ブロック有効性判断

#7.行ブロックごとの文字分け
	blocks_detail = []
	for idx in range(len(blocks)):
		list   = []
		s, e = blocks[idx]
		for x in range(width):
			t = 0
			for y in range(s, e+1):
				t = t + (1 if pix[x,y] == 0 else 0 )					
			list.append(t)
		zero_cnt = 0
		char_cnt = 0
		base_idx = 0
		detail = []
		for v in list:
			if ( v != 0 ):
				if ( zero_cnt == 1):
					char_cnt = char_cnt + 1	
				if ( zero_cnt > 1 and char_cnt >=1 ):
					detail.append((base_idx - char_cnt - zero_cnt, base_idx - zero_cnt))
					char_cnt = 0
				char_cnt = char_cnt + 1
				zero_cnt = 0	
			else:
				zero_cnt = zero_cnt + 1
			base_idx = base_idx + 1
		if ( char_cnt >= 1 ) :
			detail.append((base_idx - char_cnt - zero_cnt, base_idx - zero_cnt))
		blocks_detail.append(detail)	


#8.空白Image作成
	new_img = Image.new("L", (ow, oh), "#fff")
	new_pix = new_img.load()		
	vc = 10
	hc = 10
	ori_pix = im.load()
	for idx in range(len(blocks)):
		s, e = blocks[idx]
		h = e - s + 1
		if ( vc + h*2 > oh ):
			break
		hc = 10 
		for l, r in blocks_detail[idx]:
			g = r - l + 1
			if ( hc + g*2 > ow ):
				break
			dh = 0
			for x in range(l, r+1):
				dv = 0
				for y in range(s, e+1):
					new_pix[hc + dh, vc + dv] = ori_pix[x,y]
					new_pix[hc + dh + 1, vc + dv] = ori_pix[x,y]
					
					new_pix[hc + dh, vc + dv + 1] = ori_pix[x,y]
					new_pix[hc + dh+1, vc + dv + 1] = ori_pix[x,y]
					dv = dv + 2
				dh = dh + 2
			hc = hc + g * 2
			hc = hc + 5 
		vc = vc + h*2
		vc = vc + 10 
	new_img.save(output)

#5.5 画像表示
	nim = im.copy()
	pix = nim.load()
	idx = 0
	for s,e in blocks:
		for p in range(width):
			pix[p, s] = 0 
			pix[p, e] = 0
		for f, l in blocks_detail[idx]:
			for z in range(s, e):	
				pix[f, z ] = 0
				pix[l, z ] = 0
			
		idx = idx + 1		
	nim.save(output)

#Main処理
if __name__ == '__main__':
        argvs = sys.argv
        argc  = len(argvs)
        if ( argc != 2 ):
                print "Usage: img-tool <FileName>"
                sys.exit(1)
        splitImage(argvs[1], '/tmp/result.pgm', 640, 500)
